(function ($) {
    $.fn.extend({
        //插件名称 
        dataTable: function (options) {

            //参数和默认值
            var defaults = {
                animatePadding: 10,
                hoverColor: "Black",
                colConfig: [
                    {
                        name:"姓名",
                        colName:"name",
                        width:"100px",
                    },
                    {
                        name:"性别",
                        colName:"sex",
                        width:"100px",
                    },
                    {
                        name:"电话",
                        colName:"phone",
                        width:"100px",
                    }
                ],
                pageSizeOptions:[10, 20, 50],
                searchForm:"#searchForm",
                getSearchUrl: function() {
                    return "/struts2test/test/testAction_test";
                },
                getRecords: function (data) {return data},
                getTotalPage: function (data) {},
                getTotalRecords: function (data) {},
                appendToTbody: function (tbody) {}
            };

            var options = $.extend(defaults, options);

            return this.each(function () {
                var o = options;

                var $containerDiv = $(this).parent().find("div:first");
                
                var datatable = {
                    pageSize:10,
                    totalPage : 0,
                    page : 1,
                    init:function() {
                        //设置最外层
                        var wrapper = ['<table id="com_cernet_emall_datatable" cellpadding="0" cellspacing="0" border="0" class="tDefault tablewidth dataTable">',
                            '<thead id="com_cernet_emall_datatable_theader"><tr></tr></thead>',
                            '<tbody id="com_cernet_emall_datatable_tbody"></tbody>',
                            '</table>'
                        ];
                        
                        $containerDiv.append($(wrapper.join("")));
                        
                        //设置头部
                        (function setHeader (colConfig) {
                            var headerHtmls = new Array();
                            headerHtmls.push('<th width="20px"></th>');
                            $(colConfig).each(function() {
                                headerHtmls.push('<th width="'+this.width+'">' + this.name + "</th>");                              
                            });

                            $("#com_cernet_emall_datatable_theader").append(headerHtmls.join(""));
                        })(options.colConfig);
                        
                        //设置分页
                        (function setPaging() {
                            var pageSizeSelectOptions = new Array();
                            $(options.pageSizeOptions).each(function () {
                                pageSizeSelectOptions.push('<option>'+this+'</option>');
                            });
                            
                            var pagingHtml = [
                            '<div id="dataTables_paginate_paging_pagePan" class="fg-toolbar tableFooter">',
                                '<div id="dataTables_paginate_paging_total" class="dataTables_info"></div>',
                                '<div class="newtablehead"><span>显示条目： </span><select id="pageSizeSelect" onchange="selectOnChange()">',
                                pageSizeSelectOptions.join(""),
                                '</select></div>',
                                '<div class="dataTables_paginate_paging_full_numbers">',
                                    '<a class="first paginate_button" id="com_cernet_emall_datatable_paging_first">首页</a>',
                                    '<a id="com_cernet_emall_datatable_paging_previous" class="previous paginate_button">上一页</a> <span id="dataTables_paginate_paging_pageNumber"></span>',
                                    '<a id="com_cernet_emall_datatable_paging_next" class="next paginate_button">下一页</a>',
                                    '<a id="com_cernet_emall_datatable_paging_last" class="last paginate_button">末页</a>',
                                '</div>',
                            '</div>',
                            '<div id="com_cernet_emall_datatable_messageZone" class="dialog wait-modal"></div>  ',
                            ];
                            
                            $containerDiv.append(pagingHtml.join(""));
                            //分页部分的点击事件 开始
                            $("#com_cernet_emall_datatable_paging_first").click(function() {
                                datatable.first();
                            });
                            $("#com_cernet_emall_datatable_paging_previous").click(function() {
                                datatable.previous();
                            });
                            $("#com_cernet_emall_datatable_paging_next").click(function() {
                                datatable.next();
                            });
                            $("#com_cernet_emall_datatable_paging_last").click(function() {
                                datatable.last();
                            });
                            //分页部分的点击事件 结束
                        })();
                    },
                    
                    /**
                     * 查询
                     */
                    search : function(pageTemp){
                        page=pageTemp;
                        $("#com_cernet_emall_datatable_messageZone").html("<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img id='image' src='../images/loading.gif' title='正在加载中，请稍后……' />&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; 正在加载中，请稍后……</p>");
                        //$("#com_cernet_emall_datatable_messageZone").dialog('open');
                        var ajaxOptions = {
                                url : options.getSearchUrl()+"?page="+pageTemp+"&pageSize="+this.pageSize,
                                type : "post",
                                dataType : "json",
                                success : function(data){
                                    //$("#messageZone").dialog('close');
                                    datatable.searchCallback(data);
                                    datatable.showPage(pageTemp);
                                    
                                },
                        };
                        $(options.searchForm).ajaxSubmit(ajaxOptions);
                    },
                    
                    searchCallback : function(data){
                        var records = options.getRecords(data);
                        totalPage = options.getTotalPage(data);
                        count = options.getTotalRecords(data);
                        
                        var html = "";
                        $.each(records,function(key,val){
                            var record = this;
                            html += "<tr class='gradeX'>";
                            html += '<td style="color:red">'+(key + 1)+"</td>";
                            $(options.colConfig).each(function () {
                              html += "<td>"+ record[this.colName] || " "+"</td>";
                            });
                            html += "</tr>";
                        });
                        $("#com_cernet_emall_datatable_tbody").html("");
                        $("#com_cernet_emall_datatable_tbody").append(html);
                        options.appendToTbody($("#com_cernet_emall_datatable_tbody"));
                    },
                    previous : function(){
                        this.page = this.page - 1;
                        this.search(this.page);
                    },
                    
                    next : function(){
                        this.page = this.page + 1;
                        this.search(this.page);
                    },
                    
                    first : function(){
                        this.page = 1;
                        this.search(this.page);
                    },
                    
                    last : function(){
                        this.page = this.totalPage;
                        this.search(this.page);
                    },
                    gopage : function(pageTemp){
                        this.search(pageTemp);
                    },
                    
                    /**
                     * 每页大小选择
                     */
                    selectOnChange : function(){
                        this.search(1);
                    },
                    
                    showPage : function(pageTemp){
                        if(totalPage==0){
                            $("#dataTables_paginate_paging_pagePan").hide();
                        }else{
                            $("#dataTables_paginate_paging_pagePan").show();
                        }
                        if(totalPage==pageTemp){
                            $("#dataTables_paginate_paging_next").hide();
                        }else{
                            $("#dataTables_paginate_paging_next").show();
                        }
                        if(pageTemp==1){
                            $("#dataTables_paginate_paging_previous").hide();
                        }else{
                            $("#dataTables_paginate_paging_previous").show();
                        }
                        if(totalPage>0){
                            var html = "";
                            var endHtml = "";
                            var start = pageTemp-2;
                            var end = pageTemp+2;
                            if(start<1){
                                start = 1;
                            }
                            if(end>totalPage){
                                end = totalPage;
                            }
                            for(var i=start;i<=end;i++){
                                if(i==pageTemp){
                                    html += "<a page='"+i+"' class='paginate_active'>"+i+"</a>";
                                }else{
                                    html += "<a page='"+i+"' class='paginate_button'>"+i+"</a>";
                                }
                            }
                            html += endHtml;
                            $("#dataTables_paginate_paging_pageNumber").html("");
                            $("#dataTables_paginate_paging_pageNumber").append(html);
                            $("#dataTables_paginate_paging_pageNumber [page]").click(function () {
                                datatable.gopage($(this).attr("page"));
                            });
                        }
                        $("#dataTables_paginate_paging_total").html("共"+count+"条" + " 共"+totalPage+"页");
                    }
                };
                
                datatable.init();
                datatable.search(1);
            });
        }
    });})(jQuery);
